import yaml

def generate_compose(num_instances, base_port, config_template):
    services = {}
    for i in range(1, num_instances + 1):  # 从1开始到num_instances（包括）
        service_name = f"snmp{i}"
        services[service_name] = {
            'image': 'snmp-router',
            'build': '.',
            'ports': [f"{base_port + i}:161/udp"],  # base_port从16201开始
            'volumes': [f"{config_template}:/etc/snmp/snmpd.conf"]
        }

    compose_dict = {
        'version': '3.8',
        'services': services
    }

    # 使用 safe_dump 来避免包含 Python 具体的对象标签
    with open('docker-compose.yml', 'w') as f:
        yaml.safe_dump(compose_dict, f, default_flow_style=False)

num_instances = 100  # 想生成的实例数量
base_port = 16200   # 第一个服务的基础端口号
config_template = "./snmpd.conf"  # 配置文件路径模板

generate_compose(num_instances, base_port, config_template)
